<template>
  <div>
    <el-tree
      ref="tree"
      node-key="id"
      :data="treeList"
      :props="{label:'name'}"
      default-expand-all
      show-checkbox
      check-strictly
    />
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hAssPerm">确定</el-button>
      <el-button @click="hCancel">取消</el-button>
    </div>

  </div>
</template>

<script>
import { getPermissionList } from '@/api/permissions'
import { tranDataToTree } from '@/utils'
import { getRoleDetail, assignPerm } from '@/api/settings'
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      treeList: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async hAssPerm() {
      try {
        const permIds = this.$refs.tree.getCheckedKeys()
        await assignPerm({ id: this.id, permIds: permIds })
        this.$emit('close')
      } catch (error) {
        console.log(error)
      }
    },
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        console.log('getPermissionList', res)
        // 保存数据
        this.treeList = tranDataToTree(res.data)
        // 获取当前角色的权限点
        const info = await getRoleDetail(this.id)
        // info.data.permIds --- >
        this.$refs.tree.setCheckedKeys(info.data.permIds)
      } catch (err) {
        console.log('getPermissionList', err)
      }
    },
    hCancel() {
      // 子传父关弹层
      this.$emit('close')
    }
  }
}
</script>
